package com.yykj.drup.server.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.yykj.drup.server.dto.PrescriptionProductDto;
import com.yykj.drup.server.dto.PrescriptionStoreProductDto;
import com.yykj.drup.server.form.WestDrug;
import com.yykj.drup.server.mapper.StoreDao;
import com.yykj.drup.server.model.Store;
import com.yykj.drup.server.model.StoreProduct;
import com.yykj.drup.server.service.ProductPayOrderService;
import com.yykj.drup.server.service.ProductService;
import com.yykj.drup.server.service.StoreProductService;
import com.yykj.drup.server.service.StoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author zeng
 * @Date 2022/3/18
 */
@Service
public class ProductPayOrderServiceImpl implements ProductPayOrderService {

    //@Autowired
    //RedisTemplate redisTemplate;
    @Autowired
    private ProductService productService;
    @Autowired
    private StoreService storeService;
    @Autowired
    private StoreProductService storeProductService;
    @Autowired
     private StoreDao storeDao;


    @Override
    public List<PrescriptionStoreProductDto> findStoreProductInfoByProductIds(List<Integer> storIds, List<WestDrug> drugList) {
        List<PrescriptionStoreProductDto> storeProductDtos = new ArrayList<>();
        List<WestDrug> noHaveWestDrug = drugList;
        //获取医生常用药店药品信息
        storIds.stream().forEach(storeId -> {
            if (!noHaveWestDrug.isEmpty()){
                Map<String, List> map = getProductListByStroeId(drugList, storeId);
                if(!map.get("productDtos").isEmpty()){
                    PrescriptionStoreProductDto storeProductDto = new PrescriptionStoreProductDto();
                    //获取药店信息
                    Store store = storeService.getById(storeId);
                    BeanUtil.copyProperties(store,storeProductDto);
                    List productDtos = map.get("productDtos");
                    storeProductDto.setProducts(productDtos);
                    storeProductDtos.add(storeProductDto);
                }
                if(!map.get("westDrugs").isEmpty()){
                    noHaveWestDrug.clear();
                    noHaveWestDrug.addAll(map.get("westDrugs"));
                }else {
                    noHaveWestDrug.clear();
                }
            }
        });
        if (!noHaveWestDrug.isEmpty()){
            //获取含有指定药品含有的药店IDs
            List<Integer> productIds = noHaveWestDrug.stream().map(WestDrug::getDrugId).collect(Collectors.toList());
            List<Integer> stores = storeDao.findStoreByProductIds(productIds);
            if (stores.isEmpty()){
                throw new RuntimeException("系统数据错误，存在药品不属于任何药店。。。。");
            }
            storeProductDtos.addAll(this.findStoreProductInfoByProductIds(stores, noHaveWestDrug));
        }
        return storeProductDtos;
    }

    /**
     * 查询药店是否存在指定药品
     * @param storeId
     * @param productId
     * @return
     */
    public boolean getStoreIsHaveProductId(Integer storeId,Integer productId){
        boolean isMember = false;
            //查询数据库是否存在
            StoreProduct storeProduct = storeProductService.findByStoreIdAndProductId(storeId, productId);
            isMember = storeProduct == null ? false : true;
        //}
        return isMember;
    }

    //获取常用药店获取药品信息
    public Map<String,List> getProductListByStroeId(List<WestDrug> drugList, Integer stroeId){
        //设置返回数据
        Map<String,List> map = new HashMap<>();
        //设置返回数据
        List<PrescriptionProductDto> productDtos = new ArrayList<>();
        //设置返回数据
        List<WestDrug> westDrugs = new ArrayList<>();
        for (WestDrug westDrug : drugList) {
            boolean b = getStoreIsHaveProductId(stroeId,westDrug.getDrugId());
            if(b){
                //获取药品信息
                PrescriptionProductDto dto = productService.findProductDtById(westDrug.getDrugId());
                dto.setProductNumber(westDrug.getCnt());
                dto.setSkuName(westDrug.getSpec());
                dto.setNote(westDrug.getNote());
                productDtos.add(dto);
            }else {
                westDrugs.add(westDrug);
            }
        }
        map.put("productDtos",productDtos);
        map.put("westDrugs",westDrugs);
        return map;
    }
}
